package ru.paswd.marketprice.ui.activity

import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import kotlinx.android.synthetic.main.activity_main.*
import moxy.presenter.InjectPresenter
import ru.paswd.marketprice.R
import ru.paswd.marketprice.data.models.ProductCategory
import ru.paswd.marketprice.ui.adapter.ProductCategoryAdapter
import ru.paswd.marketprice.ui.presenter.ProductsListPresenter
import ru.paswd.marketprice.ui.view.ProductsListView
import ru.paswd.marketprice.utils.SystemUtils

class MainActivity : BaseActivity(), ProductsListView, SwipeRefreshLayout.OnRefreshListener {

    @InjectPresenter
    lateinit var presenter: ProductsListPresenter

    private var adapter: ProductCategoryAdapter? = null

    override fun initInterface() {
        products.layoutManager = LinearLayoutManager(applicationContext)
        adapter = ProductCategoryAdapter(this, listOf())
        products.adapter = adapter
        productsRefreshLayout.setOnRefreshListener(this)
        receiptScanButton.setOnClickListener(this)

        if (SystemUtils.isCameraExists(applicationContext)) {
            receiptScanButton.visibility = View.VISIBLE
        }
    }

    override fun setAttr() {
        layout = R.layout.activity_main
    }

    override fun updateCategories(categories: List<ProductCategory>) {
        adapter?.categories = categories
    }

    override fun showReceiptScanView() {
        val intent = Intent(applicationContext, ReceiptScanActivity::class.java)
        startActivity(intent)
    }

    override fun onRefresh() {
        presenter.onListRefresh()
    }

    override fun onRefreshed() {
        productsRefreshLayout.isRefreshing = false
    }

    override fun onClick(view: View?) {
        when (view?.id) {
            R.id.receiptScanButton -> presenter.onReceiptScanPressed()
        }
    }
}